/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sda.levelup.calculator.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.MenuItem;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import sda.levelup.calculator.model.ActionStorage;
import sda.levelup.calculator.model.UserAction;

/**
 *
 * @author user
 */
public class CalcFrame extends JFrame
{

    private Toolkit t = Toolkit.getDefaultToolkit();
    Dimension d = t.getScreenSize();
    private int x = (int) d.getWidth() / 2;
    private int y = (int) d.getHeight() / 2;
    private StringBuilder action = new StringBuilder();
    private ActionStorage storage = new ActionStorage();
    private UserAction userAction = new UserAction();

    public StringBuilder getAction() {
        return action;
    }

    public CalcFrame() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 300);
        setLocation(x, y);
        FillFrame();

    }

    public void FillFrame() {
        MainPanel mp = new MainPanel();
        add(mp);

    }

    class MainPanel extends JPanel
    {

        JTextField calcWindow;

        public MainPanel() {
            setLayout(new BorderLayout());
            setJMenuBar(menuBar());
            add(window(), BorderLayout.NORTH);
            add(createButtons(), BorderLayout.CENTER);
        }

        public JPanel window() {
            JPanel window = new JPanel();
            calcWindow = new JTextField(25);
            calcWindow.addKeyListener((KeyListener)new KeyAdapter()
            {
                
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        action.append(calcWindow.getText());
                        if(action.indexOf("=")<0){
                            action.append("=");
                        }                    
                    userAction = storage.createAction(action.toString());
                    calcWindow.setText(userAction.toString());
                    storage.addAction(userAction);
                    action.delete(0, action.length());
                    }
                }                
            });
            
            window.add(calcWindow);
            return window;
        }

        public JPanel createButtons() {
            JPanel buttons = new JPanel();
            buttons.setLayout(new GridLayout(4, 4));
            JButton one = new JButton("1");
            JButton two = new JButton("2");
            JButton three = new JButton("3");
            JButton four = new JButton("4");
            JButton five = new JButton("5");
            JButton six = new JButton("6");
            JButton seven = new JButton("7");
            JButton eight = new JButton("8");
            JButton nine = new JButton("9");
            JButton plus = new JButton("+");
            JButton minus = new JButton("-");
            JButton zero = new JButton("0");
            JButton multiplication = new JButton("*");
            JButton division = new JButton("/");
            JButton c = new JButton("c");
            JButton result = new JButton("=");

            class buttonAction implements ActionListener
            {

                private String num;

                public buttonAction(String n) {
                    num = n;
                }

                @Override
                public void actionPerformed(ActionEvent e) {
                    action.append(num);
                    calcWindow.setText(action.toString());
                }
            }
            one.addActionListener(new buttonAction("1"));
            two.addActionListener(new buttonAction("2"));
            three.addActionListener(new buttonAction("3"));
            four.addActionListener(new buttonAction("4"));
            five.addActionListener(new buttonAction("5"));
            six.addActionListener(new buttonAction("6"));
            seven.addActionListener(new buttonAction("7"));
            eight.addActionListener(new buttonAction("8"));
            nine.addActionListener(new buttonAction("9"));
            zero.addActionListener(new buttonAction("0"));
            plus.addActionListener(new buttonAction("+"));
            minus.addActionListener(new buttonAction("-"));
            multiplication.addActionListener(new buttonAction("*"));
            division.addActionListener(new buttonAction("/"));

            result.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e) {
                    action.append("=");
                    userAction = storage.createAction(action.toString());
                    calcWindow.setText(userAction.toString());
                    storage.addAction(userAction);
                    action.delete(0, action.length());
                }
            });
            c.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e) {
                    action.delete(0, action.length());
                    calcWindow.setText("");
                }
            });

            buttons.add(one);
            buttons.add(two);
            buttons.add(three);
            buttons.add(plus);
            buttons.add(four);
            buttons.add(five);
            buttons.add(six);
            buttons.add(minus);
            buttons.add(seven);
            buttons.add(eight);
            buttons.add(nine);
            buttons.add(multiplication);
            buttons.add(c);
            buttons.add(zero);
            buttons.add(result);
            buttons.add(division);
            return buttons;
        }

        public JMenuBar menuBar() {
            JPanel menuPanel = new JPanel();
            menuPanel.setLayout(new FlowLayout());
            JMenuBar menuBar = new JMenuBar();
            JMenu menu = new JMenu("File");
            menuBar.add(menu);

            JMenuItem menuItem = new JMenuItem("History");
            menu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e) {
                    HistoryFrame hf = new HistoryFrame(storage);
                    hf.setVisible(true);
                }
            });
            menuPanel.add(menuBar, LEFT_ALIGNMENT);
            menuPanel.setBounds(0, 0, 30, 30);
            return menuBar;
        }
    }

}
